/*
 * 用户登录系统交易
 * 参数   交易码：trancode=9001,用户名：username，用户密码:password
 * 
 */

package org.ccams.tran;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ccams.domain.User;
import org.ccams.web.UserSession;

import net.sf.json.JSONObject;

public class Tran9001 extends AbstractJsonTran {

	@SuppressWarnings("unchecked")
	public void buildJSONObject(Map model, JSONObject json,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String username = (String) request.getParameter("username");
		String password = (String) request.getParameter("password");
		username = (username != null) ? username : "";
		password = (password != null) ? password : "";
		// MD5 md5 = new MD5();  //以后完善MD5加密，以及登录验证码
		HashMap<String, Object> jsonMap = new HashMap<String, Object>();
		boolean success = false;
		String message = "";
		User u = new User();
		u.setLoginName(username);
		u.setPassWord(password);
		User user = getCcamsService().getUserDao()
				.getUserByLoginNameAndPassword(u);
		if (user != null) {
			success = true;
			message = "用户登录系统成功";
			UserSession userSession = new UserSession(user);
			request.getSession().setAttribute("userSession", userSession);
		} else {
			success = false;
			message = "用户不存在或密码不正确";
		}
		jsonMap.put("success", success);
		jsonMap.put("message", message);
		json.putAll(jsonMap);
	}
}
